﻿/**
 * @author rwecho 
 */
//
THREE.TwoDTextHelper = function (text, size, width, height, backgroundColor) {
    text = text || '0';
    width = width || 1;
    height = height || 1;
    backgroundColor = backgroundColor || 'rgba( 0, 0, 0, 0 )';

    var geometry = new THREE.PlaneGeometry(width, height);
    var material = generateLabelMaterial(text, size, backgroundColor);
   
    THREE.Mesh.call(this, geometry, material);
    this.position.x += width / 2;
    this.position.y += height / 2;
};

THREE.TwoDTextHelper.prototype = Object.create(THREE.Mesh.prototype);


function generateLabelMaterial(text,size, bg) {

    var x = document.createElement("canvas");
    var xc = x.getContext("2d");
    x.width = 128;
    x.height = 32;

    xc.fillStyle = bg;
    xc.fillRect(0, 0, 128, 32);

//    xc.shadowColor = "#000";
//    xc.shadowBlur = 3;
    xc.fillStyle = "blue";
    xc.font = size + "pt arial bold";
    xc.fillText(text, 8, 22);

    var map = new THREE.Texture(x);
    map.needsUpdate = true;

    var material = new THREE.MeshBasicMaterial({ map: map, transparent: true ,side : THREE.DoubleSide});
    return material;
}


/**
 * @author rwecho 
 */
//
THREE.THREEDTextHelper = function (parameters,geometries) {
    //text, size, height, color, font, weight, style,space //字间距。
    var text = parameters.text.toString(),
        size = parameters.size || 10,
        space = parameters.space || size/1.5,
        height = parameters.height || 0.2,
        color = parameters.color || 0xffffff,
        font = parameters.font || THREE.THREEDTextHelper.FontOptimer,
        weight = parameters.weight || THREE.THREEDTextHelper.WeightNormal;
  
    var geometry,group = new THREE.Object3D();
    var material = new THREE.MeshBasicMaterial({ color: color });
    var orginX = (text.length * height) / 2 + (text.length - 1) * space / 2;
    for (var i = 0; i < text.length; i++) {
        var t = text[i], exists = false;
        
        for (var j = 0; j < geometries.length; j++) {
            if (geometries[j].key == t) {
                geometry = geometries[j].geometry;
                exists = true;
                break;
            }
        }

        if (!exists) {
            geometry = new THREE.TextGeometry(t, {
                size: size,
                height: height,
                font: font,
                weight: weight,
            });
            geometry.computeBoundingBox();
            geometry.computeVertexNormals();
            geometries.push({ key: t, geometry: geometry });
        }
        if (geometry) {
            var mesh = new THREE.Mesh(geometry, material);
            mesh.position.x = -orginX + space * i;
            group.add(mesh);
        }
    }

    return group;
};

THREE.THREEDTextHelper.prototype = new THREE.Object3D();

THREE.THREEDTextHelper.WeightNormal = 'normal';
THREE.THREEDTextHelper.WeightBold = 'bold';
THREE.THREEDTextHelper.StyleNormal = 'normal';
THREE.THREEDTextHelper.StyleItalic = 'italic';
THREE.THREEDTextHelper.FontHelvetiker = 'helvetiker';
THREE.THREEDTextHelper.FontOptimer = 'optimer';
THREE.THREEDTextHelper.FontGentilis = 'gentilis';
THREE.THREEDTextHelper.FontDroidSans = 'droid sans';
THREE.THREEDTextHelper.FontDroidSerif = 'droid serif';



//长方形。
//width指X值，height指Z值，step步率，step不能大于width和height，形成一个 width*height的长方格
THREE.RectangleHelper = function (width, height, starta, startb, step, opacity) {
    step = step || 1;
    var geometry = new THREE.Geometry();
    var material = new THREE.LineBasicMaterial();
    material.transparent = true;
    material.opacity = opacity;
    var color = new THREE.Color(0x444444);

    for (var i = starta; i <= width; i += step) {
        for (var j = startb; j <= height; j += step) {
            geometry.vertices.push(
                new THREE.Vector3(i, 0, j), new THREE.Vector3(i, 0, height), new THREE.Vector3(i, 0, j), new THREE.Vector3(width, 0, j)
            );
            geometry.colors.push(color, color, color, color);
        }
    }
    THREE.Line.call(this, geometry, material, THREE.LinePieces);
};

THREE.RectangleHelper.prototype = Object.create(THREE.Line.prototype);
